<template>
  <a-modal
    title="新增门店数据"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <span v-if="admintype == 1">
        所属组织:
        <a-select v-model="orgCode" style="width: 120px; margin-bottom:20px" placeholder="请选择">
          <a-select-option v-for="item in orgList" :key="item.id" :value="item.code">{{ item.name }} </a-select-option>
        </a-select>
      </span>
      <a-upload-dragger :loading="excelUploadLoading" name="file" :customRequest="customRequest">
        <p class="ant-upload-drag-icon">
          <a-icon type="inbox" />
        </p>
        <p class="ant-upload-text">点击或拖拽文件到这里上传</p>
        <p class="ant-upload-hint">支持Excel表格文件</p>
      </a-upload-dragger>
    </a-spin>
  </a-modal>
</template>

<script>
import { biOperatedDataAdd, biOpreatedDataUpload } from '@/api/modular/main/bioperateddata/biOperatedDataManage'
import { getOrgList } from '@/api/modular/system/orgManage'
import { mapGetters } from 'vuex'
export default {
  computed: {
    ...mapGetters(['userInfo', 'admintype']),
  },
  data() {
    return {
      excelUploadLoading: false,
      orgList: [],
      orgCode: '',
      orgId: '',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
    }
  },
  created() {
    this.findOrgList()
    console.info(this.userInfo)
  },
  methods: {
    // 导入excel
    customRequest(data) {
      const formData = new FormData()
      formData.append('file', data.file)
      formData.append('orgCode', this.orgCode)
      formData.append('orgId',this.userInfo.orgId)
      this.excelUploadLoading = true
      biOpreatedDataUpload(formData)
        .then((res) => {
          if (res.success) {
            this.$message.success('正在导入...')
          } else {
            this.$message.warning('导入excel失败')
          }
        })
        .finally(() => {
          this.excelUploadLoading = false
        })
    },

    // orgCode列表
    findOrgList() {
      getOrgList(this.orgParams).then((res) => {
        this.orgList = res.data
      })
    },
    // 初始化方法
    add(record) {
      this.visible = true
    },
    /**
     * 提交表单
     */
    handleSubmit() {
      const {
        form: { validateFields },
      } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            if (typeof values[key] === 'object') {
              values[key] = JSON.stringify(values[key])
            }
          }
          biOperatedDataAdd(values)
            .then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败') // + res.message
              }
            })
            .finally((res) => {
              this.confirmLoading = false
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    },
  },
}
</script>
